﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using AnjLab.SyncIT.Core.Api;
using AnjLab.SyncIT.Core;

namespace AnjLab.SyncIT.App.Properties {
    
    
    // This class allows you to handle specific events on the settings class:
    //  The SettingChanging event is raised before a setting's value is changed.
    //  The PropertyChanged event is raised after a setting's value is changed.
    //  The SettingsLoaded event is raised after the setting values are loaded.
    //  The SettingsSaving event is raised before the setting values are saved.
    internal sealed partial class Settings {
        
        public Settings() {
            
            // // To add event handlers for saving and changing settings, uncomment the lines below:
            //
            // this.SettingChanging += this.SettingChangingEventHandler;
            //
            // this.SettingsSaving += this.SettingsSavingEventHandler;
            //
        }
        
        private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {
            // Add code to handle the SettingChangingEvent event here.
        }
        
        private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) {
            // Add code to handle the SettingsSaving event here.
        }

        public List<Source> SyncSources
        { get; set; }

        private static readonly XmlSerializer _sourcesSerializer = new XmlSerializer(typeof(List<Source>));

        protected override void OnSettingsLoaded(object sender, SettingsLoadedEventArgs e) {
            base.OnSettingsLoaded(sender, e);
            var filePath = GetSettingsPath();
            var sources = new List<Source>();
            if (File.Exists(filePath)) {
                using (var xmlReader = XmlReader.Create(filePath)) {
                    try
                    {
                        sources = (List<Source>)_sourcesSerializer.Deserialize(xmlReader);
                    }
                    catch
                    {
                        Library.Log.Error("Error in config {0}: {1}", filePath, e);
                    }
                }
            }
            SyncSources = sources;
        }

        private static string GetSettingsPath() {
            var path =Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"AnjLab\AnjLab.SyncIT");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            return Path.Combine(path, "sources.settings");
        }

        protected override void OnSettingsSaving(object sender, CancelEventArgs e) {
            base.OnSettingsSaving(sender, e);

            var filePath = GetSettingsPath();
            using (var xmlWriter = XmlWriter.Create(filePath)) {
                _sourcesSerializer.Serialize(xmlWriter, SyncSources);
                xmlWriter.Flush();
            }
        }
    }
}
